/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package applety;

import java.awt.Point;
import java.awt.Rectangle;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JApplet;
import javax.swing.JLabel;

/**
 *
 * @author Riso-Desktop
 */
public class mladata extends JLabel implements MouseListener, MouseMotionListener {

    private boolean taha = false;
    private int x = 0;
    private int y = 0;
    int curX = -1;
    int curY = -1;
    public Rectangle rect = new Rectangle(100, 100);
    private static int[] pozicie = {10, 115, 220, 325, 430};
    private int pozicia = 0;
    private hra1 hra;
    private static boolean vlk = false;
    private static boolean krava = false;
    private static boolean sliepka = false;
    private static boolean medved = false;
    private static boolean bocian = false;
    private int cislo = -1;
    ArrayList cisla = new ArrayList();
    private String zdroj;
    
    public mladata(String src, hra1 hraa) {
        this.hra = hra;
        zdroj = src;
        while (pozicia == 0) {
            int r = (int) (5 * Math.random()) + 0;
            if (pozicie[r] != 0) {
                pozicia = pozicie[r];
                pozicie[r] = 0;
            }
        }
        System.out.println(src);
        this.hra = hraa;
        this.setBounds(pozicia, 270, 100, 100);
        this.addMouseListener(this);
        this.addMouseMotionListener(this);
        pozicia = pozicia + 105;
        pridajIkonu();
    }
    

    private synchronized void pridajIkonu(){
        this.setIcon(new javax.swing.ImageIcon(getClass().getResource(zdroj)));
    }
    @Override
    public void mouseClicked(MouseEvent e) {
    }

    @Override
    public void mousePressed(MouseEvent e) {
        taha = true;
        Point p = e.getPoint();
        x = p.x;
        y = p.y;
    }

    @Override
    public void mouseReleased(MouseEvent e) {
        taha = false;
    }

    @Override
    public void mouseEntered(MouseEvent e) {
    }

    @Override
    public void mouseExited(MouseEvent e) {
    }

    @Override
    public void mouseDragged(MouseEvent e) {
        Point p = e.getPoint();
        curX = p.x;
        curY = p.y;
        if (taha) {
            this.setLocation(curX - x + getLocation().x, curY - y + getLocation().y);
            rect.setLocation(this.getLocation());
            //prilepenie na pozície
            if (rect.intersects(hra.bocianr) && !rect.intersects(hra.kravar) && !rect.intersects(hra.medvedr)
                    && !rect.intersects(hra.sliepkar) && !rect.intersects(hra.vlkr)) {
                this.setLocation((int) hra.bocianr.getX(), (int) hra.bocianr.getY());
                bocian = true;
                cisla.add(1);
            } else if (rect.intersects(hra.kravar) && !rect.intersects(hra.bocianr) && !rect.intersects(hra.medvedr)
                    && !rect.intersects(hra.sliepkar) && !rect.intersects(hra.vlkr)) {
                this.setLocation((int) hra.kravar.getX(), (int) hra.kravar.getY());
                krava = true;
                cisla.add(2);
            } else if (rect.intersects(hra.medvedr) && !rect.intersects(hra.kravar) && !rect.intersects(hra.bocianr)
                    && !rect.intersects(hra.sliepkar) && !rect.intersects(hra.vlkr)) {
                this.setLocation((int) hra.medvedr.getX(), (int) hra.medvedr.getY());
                medved = true;
                cisla.add(3);
            } else if (rect.intersects(hra.sliepkar) && !rect.intersects(hra.kravar) && !rect.intersects(hra.medvedr)
                    && !rect.intersects(hra.bocianr) && !rect.intersects(hra.vlkr)) {
                this.setLocation((int) hra.sliepkar.getX(), (int) hra.sliepkar.getY());
                sliepka = true;
                cisla.add(4);
            } else if (rect.intersects(hra.vlkr) && !rect.intersects(hra.kravar) && !rect.intersects(hra.medvedr)
                    && !rect.intersects(hra.sliepkar) && !rect.intersects(hra.bocianr)) {
                this.setLocation((int) hra.vlkr.getX(), (int) hra.vlkr.getY());
                vlk = true;
                cisla.add(5);
            }
            //kontrola či je všetko uložené
            for (int i = 0; i < cisla.size(); i++) {
                cislo = (int) cisla.get(i);
                switch (cislo) {
                    case 1:
                        if (!rect.intersects(hra.bocianr)) {
                            bocian = false;
                            cisla.remove(i);
                        }
                        break;
                    case 2:
                        if (!rect.intersects(hra.kravar)) {
                            krava = false;
                            cisla.remove(i);
                        }
                        break;
                    case 3:
                        if (!rect.intersects(hra.medvedr)) {
                            medved = false;
                            cisla.remove(i);
                        }
                        break;
                    case 4:
                        if (!rect.intersects(hra.sliepkar)) {
                            sliepka = false;
                            cisla.remove(i);
                        }
                        break;
                    case 5:
                        if (!rect.intersects(hra.vlkr)) {
                            vlk = false;
                            cisla.remove(i);
                        }
                        break;

                }
            }
            if (vlk && krava && sliepka && medved && bocian) {
                hra.ulozene = true;
                hra.vyhodnot();
            } else {
                hra.ulozene = false;
                hra.skry();
            }
        }
    }

    @Override
    public void mouseMoved(MouseEvent e) {
    }
}
